﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ZLDC.Common.Estate;
using System.Data.SqlClient;

namespace ZLDC.DAL.Estate
{
    public class NewsDAL
    {
                                                public static DataTable p_list_news(string condition, _Database db)
        {
            DataTable dt = null;
            try
            {
                string sql = " select * from t_news " + condition + " order by pubdate desc ";
                dt = db.Query(sql).Tables[0];

            }
            catch
            {
            }
            finally
            {
                db.close();
            }

            return dt;
        }

                                                        public static DataSet p_list_Details(int id, _Database db)
        {
            DataSet ds = null;

            try
            {
                string sql = " select * from t_news where id=" + id + " and dltflg=0";
                ds = db.Query(sql);
            }
            catch
            {
            }
            finally
            {
                db.close();
            }
            return ds;

        }

                                                        public static string InsertNews(string condition, _Database db)
        {
            string outmsg = "";
            try
            {
                int rows = db.ExecuteSql(condition.ToString());
                if (rows > 0)
                    outmsg = "发布成功";
                else
                    outmsg = "发布失败";
            }
            catch
            {
            }
            finally
            {
                db.close();
            }

            return outmsg;
        }

                                                                        public static DataTable p_news_list_table(int page, int pagesize, ref int total, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@page", page));
            parms.Add(new SqlParameter("@pagesize", pagesize));

            SqlParameter pTotal = new SqlParameter("@total", SqlDbType.Int, 4);
            pTotal.Direction = ParameterDirection.InputOutput;
            pTotal.Value = total;
            parms.Add(pTotal);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_news_list", parms);

            total = (pTotal.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pTotal.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }
                                                public static string UpdateNews(int id, string condition, _Database db)
        {
            string outmsg = "";
            try
            {
                string sSql = " update t_news set " + condition + " where id=" + id + " ";
                int rows = db.ExecuteSql(sSql);
                if (rows > 0)

                    outmsg = "操作成功";
                else
                    outmsg = "操作失败";
            }
            catch
            {
            }
            finally
            {
                db.close();
            }

            return outmsg;
        }

    }
}
